package com.zoro.barn.inner.service.documentsql.document.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zoro.barn.api.documentsql.document.BarnDocumentTestService;
import com.zoro.barn.commons.db.autopage.PageList;
import com.zoro.barn.commons.foundation.web.PageResultInfoInterface;
import com.zoro.barn.dto.documentsql.document.DocumentQueryDto;
import com.zoro.barn.inner.service.documentsql.document.entity.DocumentInfo;
import com.zoro.barn.inner.service.documentsql.document.service.DocumentInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/document-query-test")
public class BarnDocumentTestServiceController implements BarnDocumentTestService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final AtomicInteger atomicInteger = new AtomicInteger(0);
    @Value("${barn.exception:false}")
    private boolean isTest;


    private final DocumentInfoService documentInfoService;

    public BarnDocumentTestServiceController(DocumentInfoService documentInfoService) {
        this.documentInfoService = documentInfoService;
    }

    @Override
    public PageResultInfoInterface<DocumentQueryDto> getDocumentInfo(DocumentQueryDto data) throws JsonProcessingException {
        int callNum = atomicInteger.incrementAndGet();
        logger.error("测试开始了:{}", callNum);
        if (isTest || callNum > 300) {
            throw new RuntimeException("dangerous");
        }
        List<DocumentInfo> infos = documentInfoService.test();
        return PageList.fromCollection(infos, DocumentQueryDto.class);
    }
}
